<template>
  <div>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="18">
          <el-input
            v-model="notice.title"
            placeholder="请输入通知标题"
            size="medium"
          ></el-input>
        </el-col>
      </el-row>

      <div style="margin: 15px 0">
        <div style="border: 1px solid #ccc">
          <Toolbar
            style="border-bottom: 1px solid #ccc"
            :editor="editor"
            :defaultConfig="toolbarConfig"
            :mode="mode"
          />
          <Editor
            style="height: 500px; overflow-y: hidden"
            v-model="notice.content"
            :defaultConfig="editorConfig"
            :mode="mode"
            @onCreated="onCreated"
          />
        </div>
      </div>

      <div style="text-align: right">
        <el-button type="primary" @click="publishNotice">发布通知</el-button>
      </div>
    </el-card>

    <el-card style="margin-top: 20px">
      <el-table :data="noticeList" style="width: 100%">
        <el-table-column prop="title" label="标题"></el-table-column>

        <el-table-column prop="createTime" label="发布时间"> </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="viewDetail(scope.row)"
              >查看</el-button
            >
            <el-button type="text" size="small" @click="deleteNotice(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        style="margin-top: 20px; text-align: right"
        @current-change="handleCurrentChange"
        :current-page="pageNo"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-dialog title="通知详情" :visible.sync="dialogVisible" width="40%">
      <div>
        <h3>{{ selectedNotice.title }}</h3>

        <div v-html="selectedNotice.content"></div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import noticeApi from "@/api/notice";

export default {
  components: { Editor, Toolbar },
  data() {
    return {
      notice: {
        title: "",
        content: "",
      },

      noticeList: [],
      selectedNotice: {},
      dialogVisible: false,
      pageNo: 1,
      pageSize: 10,
      total: 2,

      editor: null,
      toolbarConfig: {
        toolbarKeys: [
          "bold",
          "italic",
          "underline",
          "through",
          "color",
          "bgColor",
          "fontSize",
          "fontFamily",
          "indent",
          "delIndent",
          "justifyLeft",
          "justifyRight",
          "justifyCenter",
          "justifyJustify",
          "lineHeight",
          "insertLink",
          "editLink",
          "unLink",
          "viewLink",
          "code",
          "blockquote",
          "headerSelect",
          "todo",
          "redo",
          "undo",
          "clearStyle",
          "divider",
          "emotion",
          "insertTable",
          "codeBlock",
          // ❌ 注意：不要加 'uploadImage'、'insertImage' 等图片相关按钮
        ],
      },
      editorConfig: {
        placeholder: "请输入内容...",
        MENU_CONF: {
          uploadImage: false, // 禁用上传图片
        },
      },
      mode: "simple", // or 'simple'
    };
  },
  methods: {
    getNoticeList() {
      noticeApi.getNotices(this.pageNo, this.pageSize).then((res) => {
        this.noticeList = res.data.data.rows;
        this.total = res.data.data.total;
      });
    },

    onCreated(editor) {
      this.editor = Object.seal(editor); // 一定要用 Object.seal() ，否则会报错
    },
    publishNotice() {
      console.log("发布通知：", this.notice);

      if (this.notice.title == null || this.notice.title.trim() == "") {
        this.$message.error("请输入通知标题");
        return;
      }

      noticeApi.saveNotification(this.notice).then((res) => {
        this.notice = {
          title: "",
          content: "",
        };
        this.getNoticeList();
        this.$message.success(res.data.message);
      });
    },
    viewDetail(row) {
      this.selectedNotice = row;
      this.dialogVisible = true;
    },
    deleteNotice(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          noticeApi
            .deleteNotificationById(row.id)
            .then(() => {
              this.$message({
                type: "success",
                message: "删除成功!",
              });

              this.getNoticeList();
            })
            .finally(() => {});
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleCurrentChange(page) {
      this.pageNo = page;
      this.getNoticeList();
    },
  },

  created() {
    this.getNoticeList();
  },

  mounted() {
    // 模拟 ajax 请求，异步渲染编辑器
    setTimeout(() => {
      this.html = "<p>模拟 Ajax 异步设置内容 HTML</p>";
    }, 1500);
  },
  beforeDestroy() {
    const editor = this.editor;
    if (editor == null) return;
    editor.destroy(); // 组件销毁时，及时销毁编辑器
  },
};
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style scoped>
.el-card {
  margin-bottom: 20px;
}

#editor—wrapper {
  border: 1px solid #ccc;
  z-index: 100; /* 按需定义 */
}
#toolbar-container {
  border-bottom: 1px solid #ccc;
}
#editor-container {
  height: 500px;
}
</style>
